﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace LWH.ByteProtocol
{
    /// <summary>
    /// 包装属性的具体实现，内部包含多个属性
    /// </summary>
    public class PackageProperty : OwnerProperty
    {
        /**
         * 属性列表，列表中的属性根据协议中的属性顺序排列
         */
        protected List<AbstractProperty> mPropertyList = new List<AbstractProperty>();

        public PackageProperty(OwnerProperty packageProperty) : base(packageProperty)
        {
        }


        override public int getLength()
        {
            int length = 0;

            foreach (var property in mPropertyList)
            {
                length += property.getLength();
            }

            return length;
        }


        override public bool handleProperty(Dictionary<String, Object> context)
        {
            foreach (var property in mPropertyList)
            {
                property.handleProperty(context);
            }
            return true;
        }

        protected internal override int getPropertyOffset(AbstractProperty property)
        {
            var offset = 0;
            var index = property.getIndex();

            for (var i = 0; i < index; i++)
            {
                offset += mPropertyList[i].getLength();
            }

            return offset;
        }

        public override void addProperty(AbstractProperty property)
        {
            //添加到属性列表的时候更新index，注意一个属性只能存在于一个PackageProperty，因为如果经过包装后添加到其他PackageProperty的时候，index会被改变
            property.setIndex(mPropertyList.Count());
            mPropertyList.Add(property);
            
        }



        /// <summary>
        /// 获取从起始属性到结束属性(包括结束属性)的byte值之和，一般用于计算校验和
        /// </summary>
        /// <param name="beginProperty">起始属性</param>
        /// <param name="endProperty">结束属性</param>
        /// <returns>byte值之和</returns>
        public int getByteSum(AbstractProperty beginProperty, AbstractProperty endProperty)
        {
            int sum = 0;
            int beginIndex = beginProperty.getIndex();
            int endIndex = endProperty.getIndex();
            for (int i = beginIndex; i <= endIndex; i++)
            {
                AbstractProperty property = mPropertyList[i];
                byte[] codeData = property.getData();
                for (int b = 0; b < property.getLength(); b++)
                {
                    //sum += ByteConverter.getUintByte(codeData[b]);
                    sum += codeData[b];
                }
            }
            return sum;
        }


        /// <summary>
        /// 获取从起始属性到结束属性(包括结束属性)的byte长度
        /// </summary>
        /// <param name="beginProperty">起始属性</param>
        /// <param name="endProperty">结束属性</param>
        /// <returns>byte长度之和</returns>
        public int getByteLength(AbstractProperty beginProperty, AbstractProperty endProperty)
        {
            int lengthSum = 0;
            int beginIndex = beginProperty.getIndex();
            int endIndex = endProperty.getIndex();
            for (int i = beginIndex; i <= endIndex; i++)
            {
                AbstractProperty property = mPropertyList[i];
                lengthSum += property.getLength();
            }
            return lengthSum;
        }
    }
}
